package com.jiyuren.spring.api;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author jiyuren on 2016-11-23 16:31
 * @name com.jiyure.test.com.jiyuren.main.api.OkHttpManager
 * @email 1126618648@qq.com
 * @descr
 * @copyright © www.jinmi.com
 */
public class OkHttpManager {

    private static volatile OkHttpManager mInstance;
    OkHttpClient client;

    public static OkHttpManager getInstance() {
        if (null == mInstance) {
            synchronized (OkHttpManager.class) {
                if (null == mInstance) {
                    mInstance = new OkHttpManager();
                }
            }
        }
        return mInstance;
    }

    private OkHttpManager() {
        client = new OkHttpClient.Builder()
                .addNetworkInterceptor(new HttpLogInterceptor())
                .build();
    }

    public OkHttpClient getClient() {
        return client;
    }

    public static void main(String[] args) {
        OkHttpClient client = OkHttpManager.getInstance().getClient();
        Request request = new Request.Builder()
                .url("http://127.0.0.1/testurl.php")
                .get()
                .build();

        try {
            Response response = client.newCall(request).execute();
            System.out.println(response.body().string());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
